package com.thinkgem.jeesite.modules.sys.webservice;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Element;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.mapper.JaxbMapper;
import com.thinkgem.jeesite.common.web.webservice.PagingInfo;
import com.thinkgem.jeesite.common.web.webservice.Response;

@Transactional
public abstract class AbBaseWebserviceServiceImpl<T> implements IBaseWebserviceService<T> {

	private Class<T> dataClazz;
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public AbBaseWebserviceServiceImpl() {
		try {
			Type genType = getClass().getGenericSuperclass();
			Type[] parames = ((ParameterizedType) genType).getActualTypeArguments();
			dataClazz = (Class) parames[0];
		} catch (Exception e) {
			
		}
	}
	
	public abstract Response<T> dealSubmit(HttpServletRequest request, T t,PagingInfo paginginfo);
	
	@Override
	public String dealSubmit(HttpServletRequest request, Element requestData, Element pagingData) throws Exception {
		PagingInfo pagingInfo = null;
		if(pagingData!=null){
			String pagingDataXml = pagingData.asXML();
			pagingInfo = JaxbMapper.fromXml(pagingDataXml, PagingInfo.class);
		}
		String requestDataXml = requestData.asXML();
		T t = JaxbMapper.fromXml(requestDataXml, dataClazz);
		Response<T> response =  dealSubmit(request, t, pagingInfo);
		
		String responseStr = JaxbMapper.toXml(response,dataClazz);
		return responseStr;
	}
	
}
